#============================================================
#    Item
#============================================================
# - datetime: 2022-09-30 21:49:52
#============================================================
@tool
extends MarginContainer


signal clicked
signal double_clicked
signal selected
signal deselected
signal right_clicked
signal hovered


@onready 
var label : Label = %Label
@onready
var texture_rect : TextureRect = %TextureRect
@onready 
var background = %background
var path: String : set=set_path


#============================================================
#  SetGet
#============================================================
func set_path(v:String):
	path = v
	if not self.is_inside_tree():
		await ready
	self.label.text = path.get_file()
	self.tooltip_text = path

func set_texture(v: Texture2D):
	if not self.is_inside_tree():
		await ready
	texture_rect.texture = v


#============================================================
#  内置
#============================================================
func _ready():
	mouse_entered.connect(func(): self.background.show() )
	mouse_exited.connect(func(): self.background.hide() )
	focus_entered.connect(func(): self.selected.emit() )
	focus_exited.connect(func(): self.deselected.emit() )


func _gui_input(event):
	if event is InputEventMouseButton :
		if event.pressed: 
			if event.double_click:
				double_clicked.emit()
			elif event.button_index == MOUSE_BUTTON_RIGHT:
				right_clicked.emit()


func _get_drag_data(at_position):
	var label = self.duplicate(0)
	set_drag_preview(label)
	return {
		"type": "files" if FileAccess.file_exists(path) else "files_and_dirs",
		"files": [path],
	}
	


